package TRSS

import (
	"fmt"
	"os"
	"os/exec"
	"runtime"
	"GoBox/src/Node"
	"time"
)
const (
	Red = "\033[31m Error: "
	Green = "\033[32m Success:"
	Yellow = "\033[33m Warning:"
	Reset = "\033[0m"
)

// 1.初始化/前台启动
func TRSS_init(GoBoxPath,BotPath, NodePath, FFmpegPath string) {
	if err := Node.NodeCommand(GoBoxPath,BotPath, NodePath, FFmpegPath); err != nil {
		fmt.Println("\033[31m TRSS云崽启动失败,请检查是否缺失依赖,或者Node环境...", err, "\033[0m")
		//切换GoBox目录
		// os.Chdir(GoBoxPath)
	}
	time.Sleep(3 * time.Second)
}
// 2.QQ频道账号配置
func TRSS_QQ_account_config(GoBoxPath,BotPath string) {
	var alemon_bot_menu = `
	===========QQ频道账户配置===========
	  1.添加QQ频道账号
	  2.修改QQ频道账号
	  3.删除QQ频道账号
	  4.查看所有QQ频道账号
	  0.返回主菜单
	请输入菜单编号：`
	var choice int
	for {
		clearLog()
		fmt.Printf("\033[34m")
		fmt.Print(alemon_bot_menu)
		fmt.Scanln(&choice)
		switch choice {
		case 1:
			// 1.添加QQ频道账号
			TRSS_account_add(GoBoxPath,BotPath)
		case 2:
			// 2.QQ频道账号配置
			TRSS_account_modify(GoBoxPath,BotPath)
		case 3:
			// 3.删除QQ频道账号
			TRSS_account_delete(GoBoxPath,BotPath)
		case 4:
			// 4.查看所有QQ频道账号
			TRSS_account_view(GoBoxPath,BotPath)
		case 0:
			// 返回主菜单
			return
		default:
			fmt.Println("输入错误，请重新输入")

		}

	}

}
// 3.修改/添加主人账号
func TRSS_master_modify(GoBoxPath,BotPath string) {
	var choice int
	var master_menu string = `
	=========修改/添加主人账号==========
	  1.添加QQ/频道主人
	  2.删除QQ/频道主人
	  3.多账户添加主人
	  4.多账户删除主人
	  5.查看所有
	  0.返回主菜单
	请输入菜单编号：`
	for {
		clearLog()
		fmt.Printf("\033[34m")
		fmt.Print(master_menu)
		fmt.Scanln(&choice)
		switch choice {
		case 1:
			// 1.修改主人QQ
			TRSS_addMasterQQ(GoBoxPath,BotPath)
		case 2:
			TRSS_delMasterQQ(GoBoxPath,BotPath)

		case 3:
			// 3.修改主人密码
			TRSS_addMaster(GoBoxPath,BotPath)
		case 4:
			// 4.添加主人QQ
			TRSS_deleteMaster(GoBoxPath,BotPath)
		case 5:
			// 5.添加主人账号
			TRSS_master_show(GoBoxPath,BotPath) 
		case 0:
			// 返回上一级
			return
		default:
			fmt.Println("输入错误，请重新输入")
			time.Sleep(2 * time.Second)

		}

	}
}

// 4.后台启动/停止机器人
func TRSS_bot_start_stop(GoBoxPath,BotPath, NodePath, FFmpegPath string) {
	var status_menu string = `
	========后台启动/停止机器人=========
          1.启动机器人(pnpm start)
          2.停止机器人(pnpm stop)
          3.重启机器人(pnpm restart)
          0.返回上一级
        请输入菜单编号：`
		var choice int
		for {
			clearLog()
			fmt.Printf("\033[34m")
			fmt.Print(status_menu)
			fmt.Scanln(&choice)
			switch choice {
			case 1:
				// 1.启动机器人(pnpm start)
				if err := Node.PnPmCommand(GoBoxPath,BotPath, NodePath, FFmpegPath, 1); err != nil {
					fmt.Println("\033[31m 启动失败:%s", err, "\033[0m")
				}
				time.Sleep(3 * time.Second)
			case 2:
				// 2.停止机器人(pnpm stop)
				if err := Node.PnPmCommand(GoBoxPath,BotPath, NodePath, FFmpegPath, 2); err != nil {
					fmt.Println("\033[31m 停止失败:%s", err, "\033[0m")
				}
				time.Sleep(3 * time.Second)
			case 3:
				// 3.重启机器人(pnpm restart)
				if err := Node.PnPmCommand(GoBoxPath,BotPath, NodePath, FFmpegPath, 3); err != nil {
					fmt.Println("\033[31m 重启失败:%s", err, "\033[0m")
				}
				time.Sleep(3 * time.Second)
			case 0:
				// 返回上一级
				return
			default:
				fmt.Println("输入错误，请重新输入")
				time.Sleep(3 * time.Second)
			}

		}
}
//6.查看当前日志 切换到Bot目录下 运行pnpm run log
func TRSS_bot_log(GoBoxPath,BotPath, NodePath, FFmpegPath string) {
	// 切换到Bot目录下
	if err := os.Chdir(BotPath); err != nil {
		//红色字体
		fmt.Println("\033[31m Error：切换目录失败", err, "\033[0m")
	}
	if err := Node.PnPmCommand(GoBoxPath,BotPath, NodePath, FFmpegPath, 4); err != nil {
		fmt.Println("\033[31m 查看日志失败:%s", err, "\033[0m")
	}
	time.Sleep(3 * time.Second)

}
func clearLog() {
	var cmd *exec.Cmd
	if runtime.GOOS == "windows" {
		cmd = exec.Command("cmd", "/c", "cls")
	} else {
		cmd = exec.Command("clear")
	}
	cmd.Stdout = os.Stdout
	cmd.Run()
}
